﻿using HuaCheng.CommonClass;
using HuaCheng.Controller;
using HuaCheng.Model;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace HuaCheng.UserControls
{
    public partial class UcChannelBig : UserBase
    {
        //记录上一个channel状态
        private Channel lastChannel = new Channel();
        //通道数，用于控制显示
        public int chlCount = 0;
        public new UnitOrder uOrder;
        private string chgcap = "";
        private string dchgcap = "";
        private string chgeng = "";
        private string dchgeng = "";
        #region 初始化
        public UcChannelBig(string language)
        {
            InitializeComponent();
            SetResourceCulture(language);
            CheckForIllegalCrossThreadCalls = false;//允许线程之间的操作
        }
        /// <summary>
        /// 设置语言环境
        /// </summary>
        public override void SetResourceCulture(string language)
        {
            ResourceCulture.SetCurrentCulture(language);
            this.lblStepNum.Text = ResourceCulture.GetString("lblStepNumText");
            this.lblIteNum.Text = ResourceCulture.GetString("lblIteNumText");
            this.lblVoltage.Text = ResourceCulture.GetString("lblVoltageText");
            this.lblCurrent.Text = ResourceCulture.GetString("lblCurrentText");
            this.lblStepTime.Text = ResourceCulture.GetString("lblStepTimeText");
            this.lblStepTime.Text = ResourceCulture.GetString("lblStepTimeText");
            this.lblCapacity.Text = ResourceCulture.GetString("Chg_Cap");
            this.lblEnergy.Text = ResourceCulture.GetString("Chg_Eng");
            chgcap = ResourceCulture.GetString("Chg_Cap");
            dchgcap = ResourceCulture.GetString("Dchg_Cap");
            chgeng = ResourceCulture.GetString("Chg_Eng");
            dchgeng = ResourceCulture.GetString("Dchg_Eng");
        }
        /// <summary>
        /// 从配置文件读取单位
        /// </summary>
        public override void InitParam()
        {
            uOrder = UnitOrderControl.GetUnit();
            this.lblVolUnit.Text = "(" + uOrder.Voltage + ")";
            this.lblCurUnit.Text = "(" + uOrder.Current + ")";
            this.lblEnerUnit.Text = "(" + uOrder.Capacity + ")";
            this.lblCapUnit.Text = "(" + uOrder.Energy + ")";
        }
        /// <summary>
        /// 通道数据类
        /// </summary>
        public override Channel Channel
        {
            get { return channel; }
            set
            {
                lock (value)
                {
                    //if (value.isReleaseParallel || value.isSetParallel)
                    //{
                    //    chlCount = 0;
                    //}
                    //if (chlCount == 0 || value.ChlState != lastChannel.ChlState || ( value.ChlState == Channel.runningState.Charge || value.ChlState == Channel.runningState.DisCharge))                  
                    if (value.ChlState==Channel.runningState.Finish || value.ChlState != lastChannel.ChlState || (chlCount % 100 == 0 && (value.ChlState == Channel.runningState.Charge || value.ChlState == Channel.runningState.DisCharge)))
                    {
                        try
                        {
                            channel = value;
                            this.lblCHBig.Text = value.Title == "" ? value.ChId : value.Title; ;//柜号-映射通道号
                            this.lblStepNumBig.Text = Convert.ToString(value.StepId);//工步序号
                            this.lblIterNumBig.Text = Convert.ToString(value.IterId);//循环序号
                            //this.lblStepTimeBig.Text = value.StepTime + ":" + chlCount;//时间
                            this.lblStepTimeBig.Text = value.StepTime;//时间
                            this.lblVoltageBig.Text = uOrder.Voltage == "V" ? Convert.ToString(Math.Round(value.Voltage, 4)) : Convert.ToString(Math.Round(value.Voltage * 1000, 4));//电压
                            this.lblCurrentBig.Text = uOrder.Current == "A" ? Convert.ToString(Math.Round(value.Current, 4)) : Convert.ToString(Math.Round(value.Current * 1000, 4));//电流
                            this.lblCapacityBig.Text = uOrder.Capacity == "Ah" ? Convert.ToString(Math.Round(value.Capacity, 4)) : Convert.ToString(Math.Round(value.Capacity * 1000, 4));//充电容量
                            this.lblEnergyBig.Text = uOrder.Energy == "Wh" ? Convert.ToString(Math.Round(value.Energy, 4)) : Convert.ToString(Math.Round(value.Energy * 1000, 4));//充电能量
                            this.BackColor = value.ucColor;//设置通道颜色
                            if (this.lblRunStat.Text == "完成")
                            {
                                for (int k = 0; k < 8; k++)
                                {
                                    //辅助温度1
                                    Label lblFirTempText = (Label)this.pnlAuxBig.Controls["lblFirAuxTemp" + k];
                                    lblFirTempText.Text = "";
                                    Label lblFirTempVal = (Label)this.pnlAuxBig.Controls["lblFirAuxTempVal" + k];
                                    //辅助温度2
                                    lblFirTempVal.Text = "";
                                    Label lblSecTempText = (Label)this.pnlAuxBig.Controls["lblSecAuxTemp" + k];
                                    lblSecTempText.Text = "";
                                    Label lblSecTempVal = (Label)this.pnlAuxBig.Controls["lblSecAuxTempVal" + k];
                                    lblSecTempVal.Text = "";
                                    //辅助温度3
                                    Label lblThrTempText = (Label)this.pnlAuxBig.Controls["lblThrAuxTemp" + k];
                                    lblThrTempText.Text = "";
                                    Label lblThrTempVal = (Label)this.pnlAuxBig.Controls["lblThrAuxTempVal" + k];
                                    lblThrTempVal.Text = "";
                                    //辅助电压
                                    Label lblVolText = (Label)this.pnlAuxBig.Controls["lblAuxVol" + k];
                                    lblVolText.Text = "";
                                    Label lblVolVal = (Label)this.pnlAuxBig.Controls["lblAuxVolVal" + k];
                                    lblVolVal.Text = "";
                                }
                                //int i = 0;
                                if (value.auxChannel != null)
                                {
                                    for (int i = 0; i < value.auxChannel.Count; i++)
                                    {
                                        int num = value.auxChannel[i].auxId;
                                        Label lblFirTempText = (Label)this.pnlAuxBig.Controls["lblFirAuxTemp" + i];
                                        lblFirTempText.Text = Convert.ToString("T" + num + "-1");
                                        Label lblFirTempVal = (Label)this.pnlAuxBig.Controls["lblFirAuxTempVal" + i];
                                        lblFirTempVal.Text = Convert.ToString(value.auxChannel[i].temperature1);

                                        Label lblSecTempText = (Label)this.pnlAuxBig.Controls["lblSecAuxTemp" + i];
                                        lblSecTempText.Text = Convert.ToString("T" + num + "-2");
                                        Label lblSecTempVal = (Label)this.pnlAuxBig.Controls["lblSecAuxTempVal" + i];
                                        lblSecTempVal.Text = Convert.ToString(value.auxChannel[i].temperature2);

                                        Label lblThrTempText = (Label)this.pnlAuxBig.Controls["lblThrAuxTemp" + i];
                                        lblThrTempText.Text = Convert.ToString("T" + num + "-3");
                                        Label lblThrTempVal = (Label)this.pnlAuxBig.Controls["lblThrAuxTempVal" + i];
                                        lblThrTempVal.Text = Convert.ToString(value.auxChannel[i].temperature3);

                                        Label lblVolText = (Label)this.pnlAuxBig.Controls["lblAuxVol" + i];
                                        lblVolText.Text = Convert.ToString("U" + num);
                                        Label lblVolVal = (Label)this.pnlAuxBig.Controls["lblAuxVolVal" + i];
                                        lblVolVal.Text = Convert.ToString(value.auxChannel[i].voltage);
                                    }
                                }
                            }
                            else
                            {
                                if (value.auxChannel != null)
                                {
                                    for (int i = 0; i < value.auxChannel.Count; i++)
                                    {
                                        //int num = ac.auxId;
                                        Label lblFirTempVal = (Label)this.pnlAuxBig.Controls["lblFirAuxTempVal" + i];
                                        lblFirTempVal.Text = Convert.ToString(value.auxChannel[i].temperature1);

                                        Label lblSecTempVal = (Label)this.pnlAuxBig.Controls["lblSecAuxTempVal" + i];
                                        lblSecTempVal.Text = Convert.ToString(value.auxChannel[i].temperature2);

                                        Label lblThrTempVal = (Label)this.pnlAuxBig.Controls["lblThrAuxTempVal" + i];
                                        lblThrTempVal.Text = Convert.ToString(value.auxChannel[i].temperature3);

                                        Label lblVolVal = (Label)this.pnlAuxBig.Controls["lblAuxVolVal" + i];
                                        lblVolVal.Text = Convert.ToString(value.auxChannel[i].voltage);
                                    }
                                }
                            }
                            this.lblRunStat.Text = Convert.ToString(value.ChlStepName);
                            switch (value.ChlState)
                            {
                                case Channel.runningState.Finish://完成
                                    this.lblRunStat.ForeColor = Color.Green;
                                    DefaultParam();
                                    break;
                                case Channel.runningState.Charge://充电
                                    this.lblRunStat.ForeColor = Color.Blue;
                                    this.lblCapacity.Text = chgcap;
                                    this.lblEnergy.Text = chgeng;
                                    break;
                                case Channel.runningState.DisCharge://放电
                                    this.lblRunStat.ForeColor = Color.Blue;
                                    this.lblCapacity.Text = dchgcap;
                                    this.lblEnergy.Text = dchgeng;
                                    break;
                                case Channel.runningState.Shelve://搁置
                                    this.lblRunStat.ForeColor = Color.Blue;
                                    break;
                                case Channel.runningState.Protect://保护
                                    break;
                                case Channel.runningState.Stop://停止
                                    this.lblRunStat.ForeColor = Color.Blue;
                                    DefaultParam();
                                    break;
                                case Channel.runningState.Pause://暂停
                                    break;

                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message + ex.StackTrace);
                        }
                    }
                    chlCount++;
                    lastChannel.ChlState = value.ChlState;
                }
            }
        }
        /// <summary>
        /// 点击子控件时触发父控件点击事件，避免父控件点击事件被覆盖
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public new void lbl_MouseClick(object sender, MouseEventArgs e)
        {
            base.OnMouseClick(e);
        }
        /// <summary>
        /// 点击子控件时触发父控件点击事件，避免父控件点击事件被覆盖
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public new void lbl_MouseDown(object sender, MouseEventArgs e)
        {
            base.OnMouseDown(e);
        }
        public void DefaultParam()
        {
            this.chlCount = 0;
            this.lblStepNumBig.Text = "";//工步序号
            this.lblIterNumBig.Text = "";//循环序号
            this.lblStepTimeBig.Text = "";//时间
            this.lblCurrentBig.Text = "";//电流
            //this.lblCapacity.Text = "充电容量(" + uOrder.Capacity + ")";
            //this.lblEnergy.Text = "充电能量(" + uOrder.Energy + ")";
            this.lblCapUnit.Text = "(" + uOrder.Capacity + ")";
            this.lblEnerUnit.Text = "(" + uOrder.Energy + ")";
        }
        #endregion

    }
}
